<?php

/**
 * @author Samrat Khan & Sadik Sarfaraz - 11:06:03 AM Aug 22, 2013 
 */
//require 'foo.config.php';
ob_start();
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'final_warehouse';
//$table = 'stockin';
$file = 'Faulty_return';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

//$result = mysql_query("SHOW COLUMNS FROM " . $table . "");
//if (mysql_num_rows($result) > 0) {
//    while ($row = mysql_fetch_assoc($result)) {
//        $csv_output .= $row['Field'] . ", ";
//        $i++;
//    }
//}
//$csv_output .= "\n";

//$values = selectAllMatchColumn('stockin', 'stockout', 'serialno', 'serialno', $limit, 'cus_id', $sess_cusID, $db);
$values = mysql_query("
       SELECT t1.serialno,
            (SELECT projectname FROM projects WHERE t1.project_id = projects.project_id) AS ProjectName,
            t1.rma,
            t1.pickuprequestdate,
            t1.physicallyreceivedate,
            t1.productid,
            t1.description,
            t1.revisionstate,
            t1.boxno,
            (SELECT measurement FROM uom WHERE t1.uom = uom.uom_id) AS UOM,
            t1.return_quantity,
            t1.manufacturingdate,
            (SELECT statusname FROM faultystatus WHERE t1.faultiesstatus = faultystatus.status_id ) AS FaultyStatus,
            t1.siteid,
            (SELECT warehousename FROM warehouses WHERE t1.warehouseid = warehouses.warehouse_id) AS WarehouseName,
            (SELECT employeename FROM employee WHERE t1.employeeid = employee.employee_id) AS BearerName,
            t1.docketdate,
            t1.faulties_docketnumber,
            t1.remarks,
            (SELECT username FROM wms_users WHERE t1.userid = wms_users.id) AS UserName,
            (SELECT cusname FROM customerinfo WHERE t1.cus_id = customerinfo.cus_id) AS CustomerName,
            t1.inserteddate
        FROM faulties AS t1 WHERE junkentry = '1'
        ");
$headings = array( 'Serial No', 'Project Name','Customer Request Number','Pickup Request Date','Physically Receive Date', 
                    'Product ID','Description', 'Revision State','Box No', 
                    'UOM', 'Return Quantity','Manufacturing Date','Faulty Status','Site Name','Warehouse Name',
                    'Borrower Name','Docket Date','Faulty Docket Number','Remarks','Username','Customer Name','Inserted Date');
$i = 0;
foreach ($headings as $heading) {
    $csv_output .= $heading . ", ";
    $i++;
}
$csv_output .= "\n";
while($rowr = mysql_fetch_row($values)) {
    for($j = 0; $j < $i; $j++) {
        $csv_output .= '"'.$rowr[$j] . '",';
    }
$csv_output .= "\n";
}
ob_end_clean();



$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
//header("Content-type: text/csv");
header('Content-Length: ' . strlen($csv_output) );
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>